use Wx 0.15 qw[:allclasses];
use strict;
package BioPrio;
use wxSolutionDetails;
use wxSolutionComparison;
use wxFindSolution;
use wxTreeSelector;
use wxSurvival;

use Wx qw[:everything];
use base qw(Wx::Frame);
use strict;


#Constructor
sub new {

	my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_;
    
	$parent = undef              unless defined $parent;
	$id     = -1                 unless defined $id;
	$title  = ""                 unless defined $title;
	$pos    = wxDefaultPosition  unless defined $pos;
	$size   = wxDefaultSize      unless defined $size;
	$name   = ""                 unless defined $name;


	$style = wxDEFAULT_FRAME_STYLE 
		unless defined $style;

	$self = $self->SUPER::new( $parent, $id, $title, $pos, $size, $style, $name );

	#We contain a notebook with the individual panels which are implemented in their own classes
	$self->{notebook} = Wx::Notebook->new($self, -1, wxDefaultPosition, wxDefaultSize, 0);
	$self->{panel_survival} = wxSurvival->new($self->{notebook}, -1, wxDefaultPosition, wxDefaultSize, );
	$self->{panel_tree_selector} = wxTreeSelector->new($self->{notebook}, -1);
	$self->{panel_solution_finder} = wxFindSolution->new($self->{notebook}, -1);
	$self->{panel_solution_details} = wxSolutionDetails->new($self->{notebook}, -1);
#	$self->{panel_solution_comparison} = wxSolutionComparison->new($self->{notebook}, -1);
	

	# Menu Bar
	$self->{menu} = Wx::MenuBar->new();
	$self->SetMenuBar($self->{menu});
	my $wxglade_tmp_menu;
	$self->{menu_file} = Wx::Menu->new();
	$self->{menu_load} = $self->{menu_file}->Append(wxID_OPEN, "Open project", "");
	$self->{menu_save} = $self->{menu_file}->Append(wxID_SAVE, "Save project", "");
	$self->{menu_save} = $self->{menu_file}->Append(wxID_SAVEAS, "Save project as", "");
	$self->{menu_exit} = $self->{menu_file}->Append(wxID_EXIT, '', '');
	$self->{menu}->Append($self->{menu_file}, "File");
	$self->{menu_debug} = Wx::Menu->new();
	$self->{menu_show_survival} = $self->{menu_debug}->Append(Wx::NewId(), "Show Survival Probability", "");
	$self->{menu}->Append($self->{menu_debug}, "Debug");
	$self->{menu_help} = Wx::Menu->new();
	$self->{menu_contents} = $self->{menu_help}->Append(wxID_HELP, "Help", "");
	$self->{menu_about} = $self->{menu_help}->Append(wxID_ABOUT, "About", "");
	$self->{menu}->Append($self->{menu_help}, "Help");
	
	#Set this windows properties
	$self->SetTitle("BioPrio");
	my $icon = Wx::Icon->new();
	$icon->CopyFromBitmap(Wx::Bitmap->new("../resources/penguin_icon.gif", wxBITMAP_TYPE_ANY));
	$self->SetIcon($icon);

	$self->{sizer} = Wx::BoxSizer->new(wxVERTICAL);
	$self->{sizer}->Add($self->{notebook}, 1, wxEXPAND, 0);
	$self->SetSizer($self->{sizer});
#	$self->{sizer}->Fit($self);
#	$self->Layout();

	$self->SetMinSize(Wx::Size->new(640,480));
	$self->SetSize(Wx::Size->new(800,600));

    #Bind menu events
    Wx::Event::EVT_MENU($self, $self->{menu_show_survival}->GetId, \&debug_show_survival);
    Wx::Event::EVT_MENU($self, $self->{menu_exit}->GetId, \&exit);

    #The state of the GUI, contains all properties entered by the user that need to be saved to store
    #the project
    $self->{state} = {
        'tree' => undef,
        'survival_pd' => SurvivalProbability->new,
        'survival_nap' => SurvivalProbability->new,
        'survival_gnap' => SurvivalProbability->new,
        'solutions' => undef,
        'species' => [],
    };

    #The default survival function is for phylogenetic diversity
    $self->{state}->{'survival'} = $self->{state}->{'survival_pd'};           
    
    #Pass pointers to the GUI state to the child display panels
    $self->{panel_solution_details}->set_state($self->{state});
#    $self->{panel_solution_comparison}->set_state($self->{state});
    $self->{panel_solution_finder}->set_state($self->{state});
    $self->{panel_tree_selector}->set_state($self->{state});
    $self->{panel_survival}->set_state($self->{state});
    
	#Initialise the panels
    $self->send_update_signal();

    #Return a reference to this object
    return $self
}


sub exit {
	my $self = shift;
	$self->Destroy();
}

#Print the survival objects to stdout
sub debug_show_survival {
    my ($self, $event) = @_;
    use Data::Dumper;
    print 'survival'.Dumper($self->{state}->{survival});
    print 'pd'.Dumper($self->{state}->{survival_pd});
    print 'nap'.Dumper($self->{state}->{survival_nap});
    print 'gnap'.Dumper($self->{state}->{survival_gnap});
}


#Displays an error message
sub errorMsg {
    my ($self,$caption, $msg) = @_;
    my $messagedlg = Wx::MessageDialog->new($self,
                                            $msg,
                                            $caption,
                                             wxOK || wxICON_ERROR);
    $messagedlg->ShowModal;
}

#Simple file open dialogue
sub dlg_openfile {

    my ($self, $title, $filetype) = @_;

    #Construct the dialogue
    my $filedlg = Wx::FileDialog->new(  $self,         # parent
                                    $title,   # Caption
                                    '',            # Default directory
                                    '',            # Default file
                                    $filetype, # wildcard
                                    wxOPEN);        # style

    #Get the filename
    if ($filedlg->ShowModal==wxID_OK) {
        return $filedlg->GetPath;
    } else {
        return '';
    }
}

#Simple yes no dialogue
sub dlg_yesno {

    my ($self, $title, $question) = @_;

    #Ask the question
    my $messagedlg = Wx::MessageDialog->new($self,
                                            $question,
                                            $title,
                                            wxYES_NO);
    return $messagedlg->ShowModal==wxID_YES;
}

#Return a file in one string
sub get_filestring {
    my ($self, $filename, $remove_newlines) = @_;

    #Attempt to open the file
    unless (open(INPUT, $filename)) {
        $self->errorMsg("Could not open file","The file could not be opened."); 
        return;
    }

    #Load the string into a single line
    my $string = "";
    if ($remove_newlines) { while (<INPUT>) { $string .= $_; } } 
    else { while (<INPUT>) { $string .= $_."\n"; } }
    close(INPUT) || $self->warningMsg("Could not close file","Warning: the file could not be closed.");
    return  $string;
}


#When called this function causes all panels to be updated -- should be called whenever any object contained
#in $self->state is changed  
sub send_update_signal {
	my $self = shift;
	
	my $selected_page = $self->{notebook}->GetSelection();
	
	while ($self->{notebook}->GetPageCount()) { $self->{notebook}->RemovePage(0); }

	$self->{notebook}->AddPage($self->{panel_tree_selector}, "Load Tree");
#	if (defined $self->{state}->{tree}) {
		$self->{notebook}->AddPage($self->{panel_survival}, "Survival Relationship");
		$self->{notebook}->AddPage($self->{panel_solution_finder}, "Find Solution");
#	}
	
#	if (defined $self->{state}->{solutions}) {
		$self->{notebook}->AddPage($self->{panel_solution_details}, "Solution Details");
#	}
	
	$self->{notebook}->SetSelection($selected_page);
	
	$self->{panel_solution_finder}->update();
	$self->{panel_solution_details}->update();
#    $self->{panel_solution_comparison}->update();
    $self->{panel_survival}->update();	
}

1;